package com.zhentao.mapcontroller;

import com.zhentao.domain.vo.MapVo;

public class DistanceCalculator {

//    // 地球半径，单位为千米
//    private static final double EARTH_RADIUS = 6371.0;
//
//    // 将角度转换为弧度
//    private static double toRadians(double degree) {
//        return degree * Math.PI / 180.0;
//    }
//
//    // 计算两个经纬度之间的距离，返回结果单位为千米
//    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
//        double dLat = toRadians(lat2 - lat1);
//        double dLon = toRadians(lon2 - lon1);
//
//        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
//                Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
//                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
//
//        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
//
//        return EARTH_RADIUS * c;
//    }
private static final Double PI = Math.PI;
    private static final Double PK = 180 / PI;
    private static final Double EARTH_RADII = 6370996.81;
    public static double getDistanceByCoordinate(MapVo mapVo) {
        double o_lat = mapVo.getNowY();
        double o_lng = mapVo.getNowX();

        double d_lat = mapVo.getObjectY();
        double d_lng = mapVo.getObjectX();

        double t1 = Math.cos(o_lat / PK) * Math.cos(o_lng / PK) * Math.cos(d_lat / PK) * Math.cos(d_lng / PK);

        double t2 = Math.cos(o_lat / PK) * Math.sin(o_lng / PK) * Math.cos(d_lat / PK) * Math.sin(d_lng / PK);

        double t3 = Math.sin(o_lat / PK) * Math.sin(d_lat / PK);

        double tt = Math.acos(t1 + t2 + t3);

        return EARTH_RADII * tt;
    }
}
